'use strict'

###*
 # @ngdoc service
 # @name xoceanApp.template
 # @description
 # # template
 # Service in the xoceanApp.
###
angular.module('xoceanApp')
  .value('BackendUrl', '/quickspecial/manage.do?target=')
  .service 'Template', ($q, $http, $location, BackendUrl)->
    # this.getTemplates = ->
    #   defer = $q.defer()
    #   url = "#{BackendUrl}getAllTemplates"
    #   $http.get(url).success (data)->
    #     console.log data
    #     defer.resolve(data)
    #     return
    #   return defer.promise
    this.getUserId = ->
      url = "#{BackendUrl}user"
      defer = $q.defer()
      $http.get(url).success (data)->
        defer.resolve(data.userid)
      return defer.promise

    this.getTemplates = ->
      defer = $q.defer()
      
      url = "#{BackendUrl}template&action=getAllTemplates"
      if $location.host() is 'localhost'
        defer.resolve [{"name":"模板1","id":182,"count":7,"del":0,"userid":"ybduan","ptime":1411549903000,"thumbnail":"https://cms.ws.netease.com/xocean/images/templates/template-1.jpg"},{"name":"模板2","id":183,"count":3,"del":0,"userid":"ybduan","ptime":1411550601000,"thumbnail":"https://cms.ws.netease.com/xocean/images/templates/template-2.jpg"},{"name":"模板3","id":184,"count":5,"del":0,"userid":"ybduan","ptime":1411551599000,"thumbnail":"https://cms.ws.netease.com/xocean/images/templates/template-3.jpg"},{"name":"模板4","id":202,"count":2,"del":0,"userid":"ybduan","ptime":1411613107000,"thumbnail":"https://cms.ws.netease.com/xocean/images/templates/template-4.jpg"},{"name":"模板5","id":203,"count":2,"del":0,"userid":"ybduan","ptime":1411614969000,"thumbnail":"https://cms.ws.netease.com/xocean/images/templates/template-5.jpg"}]
      else
        $http.get(url).success (data)->
          defer.resolve data
          return
      return defer.promise

    # this.getTemplateById = (id)->
    #   defer = $q.defer()
    #   url = "#{BackendUrl}getTemplate"
    #   $http.get(url, params: {id: id}).success (data)->
    #     console.log data
    #     defer.resolve(data)
    #     return
    #   return defer.promise
    this.getTemplateById = (id)->
      d = $q.defer()
      url = "#{BackendUrl}template&action=getTemplate&id=#{id}"
      if $location.host() is 'localhost'
        data = {}
        data.html = """
        <div class="ntes-html">
  <div class="cms-nav-mask"></div>
  <div class="cms-need-mask">
    <div include="'views/partials/modules/nav.html'"></div>
  </div>
  
  <div class="ntes-html-inner">
    <div include="'module.banner4'" ></div>
    <div add-module class="cms-add-module center-block ntes-hide">+</div>

    <div include="'module.main.4'" ></div>
    <div add-module class="cms-add-module center-block ntes-hide">+</div>

    <div include="'module.main.5'" ></div>
    <div add-module class="cms-add-module center-block ntes-hide">+</div>

    <div include="'module.photoset.3'" ></div>
    <div add-module class="cms-add-module center-block ntes-hide">+</div>

    <div include="'module.main.7'" ></div>
    <div add-module class="cms-add-module center-block ntes-hide">+</div>

    <div include="'module.photoset.7'" ></div>
    <div add-module class="cms-add-module center-block ntes-hide">+</div>
  </div>
  <div class="center-block">
    <div class="cms-tie-mask"></div>
    <div class="cms-need-mask">
      &lt;script&gt;var isForceShowComment = false;&lt;/script&gt;
      <div class="js-tie-vm">
        $comment
      </div>
    </div>
  </div>

  <div include="'views/partials/modules/share.html'"></div>
  <div include="'views/partials/modules/footer.html'" ></div>
</div>
"""
        d.resolve(data)
      else
        $http.get(url).success (data)->
          console.log data
          data.html = data.content
          d.resolve data
          return

      return d.promise

    # 模板使用次数+1
    this.addOne = (id)->
      url = "#{BackendUrl}template&action=incrCountById&id=#{id}"
      $http.get(url)

    this.saveCustom = (tmpl)->
      _tmpl = {}
      _tmpl.name = tmpl.name
      _tmpl.content = tmpl.html
      _tmpl.thumbnail = tmpl.thumbnail
      d = $q.defer()
      url = "#{BackendUrl}template&action=saveTemplate"
      # if $location.host() is 'localhost'
      d.resolve({result: 1})
      # else
      #   $http({
      #     method: 'POST'
      #     url: url
      #     data: _tmpl
      #   }).success (data)->
      #     console.log('Save custom template successfully!')
      #     d.resolve(data)

      return d.promise

    return this